package com.bee.beemanage.api.controller.common;

import com.bee.beemanage.api.entity.log.LogToMongo;
import com.bee.beemanage.api.entity.output.LogsOut;
import com.bee.beemanage.system.common.utils.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;

import static com.bee.beemanage.system.common.utils.result.ResultTool.success;

@RestController
@RequestMapping("/bee-manage/logs")
@Api(value ="日志查询及分析",tags = "日志查询及分析")
public class LogController {
    @Resource
    MongoTemplate mongoTemplate;
    /**
     * 字典表分页查询
     * @param current     当前页
     * @param size        一页的条数
     * @param orderColumn 排序字段
     * @param orderAsc    升降序
     * @param logToMongo   查询对象
     * @param startTime 创建起始时间
     * @param endTime 创建终止时间
     * @return 带分页信息的用户列表
     */
    @ApiOperation(value = "日志分页查询", notes = "日志分页查询")
    @GetMapping("/page")
    @PreAuthorize("hasAnyAuthority('sys:logs:page')")
    public Result page(@RequestParam(defaultValue = "0") long current, @RequestParam(defaultValue = "10") long size,
                       String orderColumn, Boolean orderAsc, LogToMongo logToMongo,
                       String startTime, String endTime) throws ParseException {
        Query query = new Query();
        Criteria criteria = new Criteria();
        if(null!=logToMongo.getLevel()){
            criteria.and("level").is(logToMongo.getLevel());
        }
        if(null!=startTime || null!=endTime){
            criteria = criteria.and("startTime");
            if(null!=startTime){
                criteria.gte(startTime+":000");
            }
            if(null!=endTime){
                criteria.lte(endTime+":000");
            }
        }
        if(null!=logToMongo.getIp()){
            criteria.and("ip").is(logToMongo.getIp());
        }
        if(null!=logToMongo.getRequestUser()){
            criteria.and("requestUser").is(logToMongo.getRequestUser());
        }
        if(null!=logToMongo.getRequestBody()){
            criteria.and("requestBody").regex(logToMongo.getRequestBody());
        }
        if(null!=logToMongo.getResponseBody()){
            criteria.and("responseBody").regex(logToMongo.getResponseBody());
        }
        if(null!=orderColumn&&null!=orderAsc){
            if(orderAsc){
                query.with(Sort.by(Sort.Direction.ASC,orderColumn));
            }else{
                query.with(Sort.by(Sort.Direction.DESC,orderColumn));
            }
        }
        query.addCriteria(criteria);
        long count = mongoTemplate.count(query, LogToMongo.class);
        query.skip(size * (current - 1)).limit((int) size);
        List<LogToMongo> logs = mongoTemplate.find(query, LogToMongo.class);
        LogsOut logsOut = new LogsOut();
        logsOut.setLogs(logs);
        logsOut.setCount(count);
        return success(logsOut);
    }
}
